#include <iostream>

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;
using namespace cv::ml;

int main(int argc, char **argv)
{
    if (argc < 2) {
        cout << "Usage: " << argv[0] << " <filename>" << endl;
        return 0;
    }

    Mat m = imread(argv[1]);
    // NOTE:此处加载会花点时间
    Ptr<KNearest> model_pixel = Algorithm::load<KNearest>("knn_pixel.yml");
    Mat temp;
    resize(m, temp, Size(70, 96));
    Mat vec1;
    vec1.push_back(temp.reshape(1, 1));
    vec1.convertTo(vec1, CV_32F);
    int r1 = model_pixel->predict(vec1); // 对所有行进行预测
    cout << (r1 >> 4) << ":" << (r1 & 0x0f) << endl;

    switch ((r1 & 0x0f)) {
    case 0:
        cout << "\033[31m♥";
        break;
    case 1:
        cout << "\033[30m♠";
        break;
    case 2:
        cout << "\033[31m♦";
        break;
    case 3:
        cout << "\033[30m♣";
        break;
    case 4:
        cout << "\033[31m大王"
             << "\033[0m" << endl;
        return 0;
    case 5:
        cout << "\033[30m小王"
             << "\033[0m" << endl;
        return 0;
    case 6:
        cout << "\033[31m万能牌"
             << "\033[0m" << endl;
        return 0;
    default:
        cout << "unknow:";
        break;
    }

    switch (int num = (r1 >> 4)) {
    case 1:
        cout << "A";
        break;
    case 11:
        cout << "J";
        break;
    case 12:
        cout << "Q";
        break;
    case 13:
        cout << "K";
        break;
    default:
        cout << num;
        break;
    }
    cout << "\033[0m" << endl;
    return 0;
}